<?php
/**
 * search success
 *
 * @package    FinalsClub
 * @subpackage search
 * @author     Masumi
 */
?>
<div id="wrapper" class="search">
	<div id="content">
		<div id="main">
			<div class="main-heading">
					<p><?php echo (count($notes) + count($courses) + count($books) + count($annotations)) ?> results for</p>
					<h1><?php echo $query ?></h1>
			</div>
			<div id="sections">
				<h4 class="center-border"><span>Sections</span></h4>
				<ul>
					<li><a href="#classes">Classes</a>(<?php echo count($courses) ?>)</li>
					<li><a href="#notes">Course Notes</a>(<?php echo count($notes) ?>)</li>
					<li><a href="#books">Books</a>(<?php echo count($books) ?>)</li>
					<li><a href="#annotations">Annotations</a>(<?php echo count($annotations) ?>)</li>
				</ul>
			</div>
			<div class="filter-links with-icon">
				<ul class="courses inline">
					<li><a name="classes"></a>Related Classes</li>
				</ul>
			</div>
			
			<ul class="courses-list">
				<?php if($courses) : ?>
					<?php $cnt = 1; ?>
					<?php foreach($courses as $course) : ?>
						<li>
							<h3>	<?php echo $course->subject ?>:
								<?php echo link_to($course->name, 'courses/show?id='.$course->pk) ?>
							</h3>
							<div class="details">
								<p class="related-users"><strong>Instructor: </strong>
										<?php echo $course->instructor_name ?>
							</div>
						</li>
						<?php if($cnt++ > 5) break; ?>
					<?php endforeach ?>	
				<?php endif; ?>
			
			<?php if(count($courses) > 5) : ?>
			<div>
				<p style="text-align: right;">
				<a href="<?php 	echo "/search/show?type=".Search::COURSE."&query=".$query."&resid=".$courses_resid ?>"  style="text-decoration:none">
					<?php echo "See all ".count($courses)." classes" ?> >>
				</a>
				</p>
			</div>
			<?php endif; ?>
			</ul>
			<div class="filter-links with-icon">
				<ul class="book inline">
					<li><a name="notes"></a>Related Class Notes</li>
				</ul>
			</div>
			<ul class="courses-list notes">
				<?php if($notes) : ?>
					<?php $cnt = 1; ?>
					<?php foreach($notes as $note) : ?>
						<li>
							<h3> <strong><?php echo $note->course ?>:</strong>
								<?php echo link_to($note->topic, 'notes/show?id='.$note->pk) ?>
							</h3>
							<p class="date">	
							<?php 
								$datetime = new DateTime($note->created);
								echo $datetime->format('F j, Y');
							?>
							</p>
						</li>
						<?php if($cnt++ > 5) break; ?>
					<?php endforeach ?>	
				<?php endif; ?>
			
			<?php if(count($notes) > 5) : ?>
			<div>
				<p style="text-align: right;">
				<a href="<?php 	echo "/search/show?type=".Search::NOTE."&query=".$query."&resid=".$notes_resid ?>"  style="text-decoration:none">
					<?php echo "See all ".count($notes)." notes" ?> >>
				</a>
				</p>
			</div>
			<?php endif; ?>
			</ul>

			<div class="filter-links with-icon">
				<ul class="annotations inline">
					<li><a name="books"></a>Related Books</li>
				</ul>
			</div>

			<ul class="annotations-list">
				<?php if($books) : ?>
					<?php $cnt = 1; ?>
					<?php foreach($books as $book) : ?>
						<li>
							<h4><?php echo link_to(stripslashes($book->title), 'books/showTOC?id='.$book->pk) ?>
							<span>by <?php echo stripslashes($book->author) ?></h4>
						</li>
					<?php if($cnt++ > 5) break; ?>
					<?php endforeach ?>	
				<?php endif; ?>
				<?php if(count($books) > 5) : ?>
					<div>
						<p style="text-align: right;">
				<a href="<?php 	echo "/search/show?type=".Search::BOOK."&query=".$query."&resid=".$books_resid ?>"  style="text-decoration:none">
							<?php echo "See all ".count($books)." books" ?> >>
							</a>
						</p>
					</div>
				<?php endif; ?>
			</ul>

			<div class="filter-links with-icon">
				<ul class="annotations inline">
					<li><a name="annotations"></a>Related Annotations</li>
				</ul>
			</div>

			<ul class="annotations-list">
				<?php if($annotations) : ?>
					<?php $cnt = 1; ?>
					<?php foreach($annotations as $annotation) : ?>
						<?php $annote = AnnotationPeer::retrieveByPK($annotation->pk); ?>
						<li>
							<?php if($annote->getSourceType() == AnnotationPeer::NOTE_SOURCE) : ?>
								<?php $note = NotePeer::retrieveByPK($annote->getNoteId()) ?>
								<?php $course = CoursePeer::retrieveByPK($note->getCourseId()) ?>
								<h4>
									<?php 
										if(EntitlementPeer::isEntitled($note->getId(), $whoami, EntitlementPeer::NOTE_TYPE)) {
											echo link_to(stripslashes($note->getTopic()), 'notes/show?id='.$note->getId());
										} else {
											echo stripslashes($note->getTopic());
										}
									?>				
								<?php $who = UserPeer::retrieveByPK($note->getUserId()) ?>
								<span>by <?php echo stripslashes($who->getUserName()) ?></span></h4>
							<?php elseif($annote->getSourceType() == AnnotationPeer::BOOK_SOURCE) : ?>
								<?php $booksection = SectionPeer::retrieveByPK($annote->getBookSectionId()) ?>
								<?php $book = BookPeer::retrieveByPK($booksection->getBookId()) ?>
								<h4><?php echo link_to(stripslashes($book->getTitle()), 'books/showTOC?id='.$book->getId()) ?>
								<span>by <?php echo stripslashes($book->getAuthor()) ?></span></h4>
								<div class="inner">
									<blockquote><cite>
									<?php echo link_to(stripslashes($booksection->getName()), 'books/showSection?id='.$booksection->getId().'&bookid='.$book->getId()) ?>
									</cite></blockquote>
								</div>
							<?php elseif($annote->getSourceType() == AnnotationPeer::FILE_SOURCE) : ?>
								<?php $file = FilePeer::retrieveByPK($annote->getFileId()) ?>
								<h4>
								<?php 
									if(EntitlementPeer::isEntitled($file->getId(),$whoami,EntitlementPeer::FILE_TYPE)) {
										echo link_to($file->getName(), $file->getAnnotateFileViewURL());
									} else {
										echo $file->getName();
									}
								?>
								<?php $who = UserPeer::retrieveByPK($file->getOwnerId()) ?>
								<span>by <?php echo stripslashes($who->getUserName()) ?></span></h4>
							<?php else : ?>
								<h4><?php echo stripslashes($annotation->subject) ?></h4>
							<?php endif; ?>
							<div class="inner">
								<blockquote>
								<?php 
									if(strlen(stripslashes($annotation->text)) > 160) {
										echo htmlspecialchars(substr(stripslashes($annotation->text),0,155),ENT_NOQUOTES) . ' ....';
									} else {
										echo stripslashes($annotation->text);
									}
								?>
								</blockquote>
							</div>
						</li>
					<?php if($cnt++ > 5) break; ?>
					<?php endforeach ?>	
				<?php endif; ?>
				<?php if(count($annotations) > 5) : ?>
					<div>
						<p style="text-align: right;">
				<a href="<?php 	echo "/search/show?type=".Search::ANNOTATION."&query=".$query."&resid=".$annotations_resid ?>"  style="text-decoration:none">
							<?php echo "See all ".count($annotations)." annotations" ?> >>
							</a>
						</p>
					</div>
				<?php endif; ?>
			</ul>

		</div>
	</div>
	<div id="sidebar">
		<div class="module members">
			<h3 class="gradient dark">Users Related to Your Search</h3>
			<div class="inner">
				<ul>
					<?php foreach($users as $user): ?>
					<li>
						<div class="info">
							<h4><?php echo link_to($user->username, 'user_profiles/show?id='.$user->pk) ?></h4>
						</div>
					</li>
					<?php endforeach ?>
				</ul>
			</div>
		</div>
		<div class="module files">
			<h3 class="gradient dark">Files Related to Your Search</h3>
			<div class="inner">
				<ul>
					<?php foreach($files as $file): ?>
					<li class="text">
						<?php $fileobj = FilePeer::retrieveByPK($file->pk); ?>
						<h4><?php echo link_to($file->name, $fileobj->getAnnotateFileViewURL()) ?></h4>
						Description: <?php echo $file->description ?>
					</li>
					<?php endforeach ?>
				</ul>
			</div>
		</div>
		<div class="module groups">
			<h3 class="gradient dark">Groups Related to Your Search</h3>
			<div class="inner">
				<ul>
					<?php $userob = UserPeer::retrieveByPK($whoami); ?>
					<?php foreach($groups as $group): ?>
					<?php $groupob = GroupPeer::retrieveByPK($group->pk); ?>
					<?php if($groupob->getNetworkId() == $userob->getNetworkId()) : ?>
					<li>
						<h4><?php echo link_to($group->name, 'groups/show?id='.$group->pk) ?></h4>
						Description: <?php echo $group->description ?>
					</li>
					<?php endif; ?>
					<?php endforeach ?>
				</ul>
			</div>
		</div>
	</div>
</div>
